import request from './request.js';
import api from './apiUrl.js';
import {
	getStorageSync,
	setStorageSync
} from './cache.js';
import $const from './const.js';
import {
	getUrlCode,
	getWeChatCode
} from './utils.js';

const storageKey = $const.USER_TOKEN_KEY;

let userInfo = null;




export default {
	login: (userInfoResult) => {
		return new Promise((resolve, reject) => {

			console.log('getUserInfoResolve->', userInfoResult);
			uni.showLoading({
				mask: true,
				title: '正在登录',
			});
			uni.login({
				scopes: 'auth_base',
				success(loginResult) {
					const data = {
						// encryptedData: userInfoResult.detail.encryptedData,
						// iv: userInfoResult.detail.iv,
						// rawData: userInfoResult.detail.rawData,
						// signature: userInfoResult.detail.signature,
						userInfo: userInfoResult.detail.rawData,
						code: loginResult.code,
					};
					uni.hideLoading();
					
					request({
						url: api.user.login,
						method: 'post',
						data: data,
					}).then(response => {
						uni.hideLoading();
						console.log(response)
						if (response.code == 0) {

							// 登陆事件
							uni.$emit($const.EVENT_USER_LOGIN, response.data)
							setStorageSync(storageKey, response.data.token);
							
							return resolve(response.data.token);
						} else {
							return reject(response.msg);
						}
					}).catch(e => {
						uni.hideLoading();
						reject(e);
					});
				}
			})
		});
	},
	getAccessToken(options) {
		options = options || {};
		return new Promise((resolve, reject) => {
			let accessToken = getStorageSync(storageKey);
			if (accessToken) {
				return resolve(accessToken);
			}
			reject(e);
		});
	},
	//微信公众号登录
	wxLogin:() => {
		let that = this;
		let code = getUrlCode('code');
		let accessToken = getStorageSync(storageKey);
		console.log('get----?code-->',code,accessToken)
		if(code && !accessToken){
			uni.showLoading({
				mask: true,
				title: '正在登录',
			});
			request({
				url: api.app.wxLogin,
				method: 'get',
				data: {
					code:code
				},
			}).then(info => {
				uni.hideLoading();
				if(info.code == 1){
					
					setStorageSync(storageKey, info.data.token);
					console.log('loginByToken',info);
				}else{
					uni.showToast({
						title: '登录失败',
					});
				}
				
			}).catch(error => {
				uni.hideLoading();
				console.log(error)
			})
		}
	
	},
	
	/**
	 *  refresh 强制刷新userinfo
	 * @param {Object} options
	 */
	getInfo(options) {
		options = options || {};
		if (typeof options.refresh === 'undefined') {
			options.refresh = false;
		}
		return new Promise((resolve, reject) => {
			if (options.refresh) {
				userInfo = null;
			}
			if (userInfo) {
				return resolve(userInfo);
			}
			this.getAccessToken().then(accessToken => {
				uni.showNavigationBarLoading();
				request({
					url: api.user.user_info,
				}).then(response => {
					uni.hideNavigationBarLoading();
					if (response.code == 0) {
						userInfo = response.data;
						uni.$emit($const.EVENT_USER_REGISTER, userInfo);
						setStorageSync($const.USER_INFO, userInfo);
						
						return resolve(userInfo);
					} else {
						return reject(response.msg);
					}
				}).catch(e => {
					uni.hideNavigationBarLoading();
					return reject(e);
				});
			}).catch(e => {
				return reject(e);
			});
		});
	},
	isLogin() {
		return !!getStorageSync(storageKey);
	},
	loginByToken(token) {
		setStorageSync(storageKey, token);
	},
}
